import http from '@/http/httpClient'
import {useUserStore} from '@/store'
import {LoginData, LoginResult} from '@/api/types'
import ApiClient from '@/http/httpClient'
import {UserInfo} from '@/api/system/system/user/types'
import {MemberVO} from '@/api/member/types'

/**
 * 登录
 * @param data
 */
export const loginApi = async (data: LoginData) => {
    const params = {
        ...data,
        clientId: import.meta.env.VITE_APP_CLIENT_ID || '428a8310cd442757ae699df5d894f051',
        grantType: data.grantType || 'member'
    }
    const res: any = await ApiClient.post<LoginResult>('/auth/memberLogin', {
        header: {
            isToken: false,
            isEncrypt: true,
            repeatSubmit: false
        },
        data: params
    })
    if (res.code === 200 && res.data.access_token != null) {
        const store = useUserStore()
        const token = res.data.access_token
        store.setAccessToken(res.data.access_token)
        const memberId = res.data.memberId
        const memberInfoRes = await getUserInfoApi()
        // const userInfo<UserInfo> = {};
        if (memberInfoRes.code === 200) {
            store.setUserInfo(memberInfoRes.data)
        }
        return true
    } else {
        console.error('登录失败:', res.msg)
    }
    return false
}

/**
 * 查询会员详细
 *
 */
export const getUserInfoApi = async () => {
    return await ApiClient.get<UserInfo>('/auth/getMemberInfo')
}
